/**
 * 验证码相关工具函数
 */

// 验证手机号格式
export function validatePhoneNumber(phone) {
	const phoneRegex = /^1[3-9]\d{9}$/;
	return phoneRegex.test(phone);
}

// 生成随机验证码
export function generateVerificationCode() {
	return Math.floor(100000 + Math.random() * 900000).toString();
}

// 模拟发送验证码
export function sendVerificationCode(phone) {
	return new Promise((resolve, reject) => {
		// 模拟网络请求延迟
		setTimeout(() => {
			if (validatePhoneNumber(phone)) {
				const code = generateVerificationCode();
				console.log(`验证码已发送到 ${phone}，验证码：${code}`);
				resolve({
					success: true,
					message: '发送成功',
					code: code
				});
			} else {
				reject({
					success: false,
					message: '请输入正确的电话号码'
				});
			}
		}, 1000);
	});
}

// 验证验证码
export function verifyCode(inputCode, correctCode) {
	return inputCode === correctCode;
}

// 密码强度检查
export function checkPasswordStrength(password) {
	if (!password) return 0;
	
	let strength = 0;
	if (password.length >= 6) strength++;
	if (/[a-z]/.test(password)) strength++;
	if (/[A-Z]/.test(password)) strength++;
	if (/[0-9]/.test(password)) strength++;
	if (/[^a-zA-Z0-9]/.test(password)) strength++;
	
	return strength;
}

// 获取密码强度标签
export function getPasswordStrengthLabel(strength) {
	if (strength <= 1) return '弱';
	if (strength <= 3) return '中';
	return '强';
}

// 获取密码强度样式类
export function getPasswordStrengthClass(strength) {
	if (strength <= 1) return 'weak';
	if (strength <= 3) return 'medium';
	return 'strong';
} 